const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    oldPass: "",
    newPass: "",
    rePass: "",
    modal: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {

  },
  //监听旧密码输入
  inputOldPass(e) {
    const value = e.detail.value;
    this.setData({
      oldPass: value
    })
  },


  //监听新密码输入
  inputNewPass(e) {
    const value = e.detail.value;
    this.setData({
      newPass: value
    })
  },

  //监听重复密码输入
  inputRePass(e) {
    const value = e.detail.value;
    this.setData({
      rePass: value
    })
  },

  //点击确定
  confirm() {
    let oldPass = this.data.oldPass.trim();
    let newPass = this.data.newPass.trim();
    let rePass = this.data.rePass.trim();
    if (oldPass === "") {
      app.showToast("请输入旧密码");
      return;
    }
    if (newPass === "") {
      app.showToast("请输入新密码");
      return;
    }
    if (rePass === "") {
      app.showToast("请重复新密码");
      return;
    }
    if (newPass !== rePass) {
      app.showToast("两次密码不一致");
      return;
    }

    this.setData({
      modal: true
    })
  },

  //modal的按钮点击
  handleClick(e) {
    const index = e.detail.index;
    if (index == 1) {
      app.ajax({
        url: 'Manage/User/savePass',
        method: "POST",
        data: {
          oldpass: this.data.oldPass,
          password: this.data.newPass,
          rpassword: this.data.rePass
        },
        success: res => {
          if (res.data.code == 200) {
            app.showToast(res.data.message);
            wx.setStorageSync('token', '');
            setTimeout(function() {
              wx.redirectTo({
                url: '/pages/login/login',
              })
            }, 1000);
          } else {
            app.showToast(res.data.message);
          }
        }
      })
    } else if (index == 0) {
      this.hide();
    }
  },

  //隐藏modal
  hide() {
    this.setData({
      modal: false
    })
  },
})